const app = getApp()
import activeApi from '../../api/activeApi'
import videoAd from '../../util/videoAd'
import {getFishNames} from '../../util/fish'
import {getLocation, chooseLocation} from '../../util/location'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeList : [],
    isMore:true,
    type: '',
    keyword: '',
    address: '',
    addressName: ''
  },
  pageData: {
    pageNum : 0,
    pageSize : 8
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
   
 

    await getLocation(true)
    this.setData({
      address: wx.getStorageSync('address'),
      addressName: wx.getStorageSync('addressName')
    })
    this.getMyActive()
   
  },

  onShow: function () {
    this.getTabBar().setData({
      selected: 0
    })

    var addActive = wx.getStorageSync('addActive')
    wx.removeStorageSync('addActive')
    if(addActive){
      this.pageData.pageNum = 0
      this.setData({
        isMore: true,
        activeList:  []
      })
     
      this.getMyActive()
    }
  },

  async choose(){
    await chooseLocation()
    this.setData({
      address: wx.getStorageSync('address'),
      addressName: wx.getStorageSync('addressName')
    })
    this.pageData.pageNum = 0
    this.setData({
      activeList: [],
      isMore: true
    })
    this.getMyActive()
  },

  toAdd(){
    wx.navigateTo({
      url: '/pages/addActive/addActive?action=add'
    })
  },

  search(e){

    this.data.keyword = e.detail.value

    this.pageData.pageNum = 0
    
    this.setData({
      activeList: [],
      isMore: true
    })
      
    this.getMyActive()
  },

  getMyActive(){
    console.log('getMyActive...')
    if(!this.data.isMore){
      return
    }
    this.pageData.pageNum ++

    var params = {
      pageNum : this.pageData.pageNum,
      pageSize : this.pageData.pageSize,
      lng: wx.getStorageSync('longitude'),
      lat: wx.getStorageSync('latitude'),
      
    }
    if(this.data.keyword){
      params.content = this.data.keyword
    }

    wx.showLoading({
      mask: true
    })
    activeApi.near(params).then(res => {
      wx.hideLoading()
      console.log(res)
      if(res.length < this.pageData.pageSize){
        this.setData({
          isMore:false
        })
      }
      for(var item of res){
        if (typeof item.imgList == 'string'){
          item.imgList = item.imgList.split(',')
        }
        item.fishNames = getFishNames(item.category)
      }
      var activeList = this.data.activeList.concat(res)
      this.setData({
        activeList:activeList
      })
    })
  },

  getMyJoinActive(){
    console.log('getMyJoinActive...')
    if(!this.data.isMore){
      return
    }
    this.pageData.pageNum ++
    console.log( this.pageData.pageNum)
    activeApi.getMyJoinActive({
      pageNum : this.pageData.pageNum,
      pageSize : this.pageData.pageSize
    }).then(res => {
      console.log(res)
      if(res.length < this.pageData.pageSize){
        this.setData({
          isMore:false
        })
      }
      var activeList = res.map((obj) => {
        return obj.activeList[0]
      })
      for(var item of activeList){
        if (typeof item.imgList == 'string'){
          item.imgList = item.imgList.split(',')
        }
      }
      var list = this.data.activeList.concat(activeList)
      this.setData({
        activeList:list
      })
    })
  },

  previewImage(e){
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    
    wx.previewImage({
      urls: list,
      current: img
    })
  },

  toDetail(e){
    var active = e.currentTarget.dataset.active
   
    wx.navigateTo({
      url: '/pages/activeDetail/activeDetail?activeId='+active._id,
    })
  },

  previewImage(e) {
    var url = e.currentTarget.dataset.url
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      urls: list,
      current: url
    })
  },

  toUpdate(e){
    var active = e.currentTarget.dataset.active

    wx.navigateTo({
      url: `/pages/addActive/addActive?activeId=${active._id}&action=update`,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.pageData.pageNum = 0
    
    this.setData({
      activeList: [],
      isMore: true
    })
      
    this.getMyActive()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMyActive()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '钓鱼圈',
      // imageUrl: 'https://public-server-center-prod-1258963190.cos.ap-guangzhou.myqcloud.com/studentCard/20210521153238.jpg'
    }
  }
})